<template>
  <el-dialog
    :title="!form.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    @closed="dialogClose"
    width="600px"
  >
    <el-form
      :model="form"
      :rules="rules"
      size="mini"
      ref="form"
      @keyup.enter.native="submitForm()"
      label-width="80px"
    >
      <el-form-item label="名称" prop="name">
        <el-input v-model="form.name" placeholder="组名"></el-input>
      </el-form-item>
      <el-form-item label="组icon" prop="icon">
        <material-library :width="100" :height="100" :num="1" v-model="form.icon"></material-library>
      </el-form-item>
      <el-row>
        <el-col :span="12">
          <el-form-item label="所属分类" prop="categoryId">
            <category-selector :categoryPath.sync="categoryPath"></category-selector>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="排序" prop="sort">
            <el-input-number v-model="form.sort" placeholder="排序"></el-input-number>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="备注" prop="remark">
        <el-input type="textarea" v-model="form.remark" placeholder="描述"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" size="mini" icon="el-icon-circle-check" @click="submitForm()">确定</el-button>
      <el-button @click="visible = false" size="mini" icon="el-icon-circle-close">取消</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { categoryTree } from '@/api/product/category'
import { getAttrGroup, saveAttrGroup, updateAttrGroup } from '@/api/product/attrGroup'
import MaterialLibrary from "@/components/ImageLibrary/index.vue";
import CategorySelector from "@/views/common/categorySelector.vue";

export default {

  components: {CategorySelector, MaterialLibrary},

  data() {
    return {
      props: {
        value: 'catId',
        label: 'name',
        children: 'children'
      },
      visible: false,
      categoryList: [],
      categoryPath: [],
      form: {
        sort: 1
      },
      rules: {
        name: [
          { required: true, message: '请填写名称', trigger: 'blur' }
        ],
        categoryId: [
          { required: true, message: '请选择分类', trigger: 'blur' }
        ]
      }
    }
  },
  watch: {
    categoryPath(val) {
      this.form.categoryId = val[val.length - 1]
    }
  },
  methods: {
    dialogClose() {
      this.catelogPath = []
    },
    listCategory() {
      categoryTree().then(res => {
        this.categoryList = res.data
      })
    },
    add(categoryPath) {
      this.categoryPath = categoryPath
      this.form = {
        sort: 1
      }
      this.visible = true
    },
    edit(id) {
      this.form.id = id
      this.visible = true
      this.$nextTick(() => {
        this.$refs['form'].resetFields()
        getAttrGroup(this.form.id).then(res => {
          if (res.success) {
            this.form = res.data
            this.categoryPath = res.data.categoryPath
          }
        })
      })
    },
    // 表单提交
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.form.categoryId = this.categoryPath[this.categoryPath.length - 1]
          if (!this.form.id) {
            saveAttrGroup(this.form).then(res => {
              if (res.success) {
                this.$message.success('新增成功')
                this.visible = false
                this.$emit('ok')
              }
            })
          } else {
            updateAttrGroup(this.form).then(res => {
              if (res.success) {
                this.$message.success('更新成功')
                this.visible = false
                this.$emit('ok')
              }
            })
          }
        }
      })
    }
  },
  created() {
    this.listCategory()
  }
}
</script>
